#include "GraphicsView.h"

#include "CustomLabel.h"
#include "customproxy.h"

#include <QScrollBar>

const int imageHeight = 300;
const int imageWidth = 300;

QImage MyScale(const QString &imageFileName )
{
	QImage image ( imageFileName );
	return image.scaled ( QSize ( imageWidth, imageHeight ), Qt::KeepAspectRatio, Qt::SmoothTransformation );
}

GraphicsView::GraphicsView(QWidget * parent ):QGraphicsView(parent)
{
	this->setScene ( &scene );

	this->setRenderHints ( QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing );

	QBrush brush(QColor(0,0,0));
	this->setBackgroundBrush(brush);

	this->setCacheMode ( QGraphicsView::CacheNone );
	this->setViewportUpdateMode ( QGraphicsView::FullViewportUpdate );


	timeLine = new QTimeLine ( 1000,this );
	timeLine->setFrameRange(-10,10);
	timeLine->setCurveShape(QTimeLine::EaseInOutCurve);


	timeLine->setLoopCount ( 1 );
	//timeLine->setCurveShape(QTimeLine::EaseInCurve); horizontalLayout->setMargin(0);

	connect ( timeLine, SIGNAL ( frameChanged ( int ) ),
	          this, SLOT ( updateStep ( int ) ) );
	connect ( timeLine, SIGNAL ( stateChanged ( QTimeLine::State ) ),
	          this, SLOT ( stateChanged ( QTimeLine::State ) ) );
	connect ( timeLine, SIGNAL ( finished() ),
	          this, SLOT ( finished() ) );



	x=0;
	y=0;

	for(int i=0;i<1;++i)
	{
		addContents();
	}
}

GraphicsView::~GraphicsView(void)
{
}

void GraphicsView::addContents()
{
	if(x >= 4)
	{
		x =0;
		y ++;
	}
		CustomLabel *label;
//	for ( int y = 0; y < column; ++y )
//	{
//		for ( int x = 0; x < row; ++x )
//		{
			CustomProxy *proxy = new CustomProxy ( 0, Qt::Window );
			
	
			label = new CustomLabel();
			label->setImage (MyScale("bugs_bunny.jpg"));
			//gridLayout->addWidget(proxy, y, x);
			//proxy->setWidget(new EmbeddedDialog);
			//label->setImage(img);
			proxy->setWidget ( label);
			
			QRectF rect = proxy->boundingRect();
			proxy->setPos ( y * rect.width() * 1.05, x * rect.height() * 1.05 );
			//proxy->setPos(x * rect.width() , y * rect.height() );
			proxy->setCacheMode ( QGraphicsItem::NoCache );

			scene.addItem ( proxy );

	scene.setSceneRect ( scene.itemsBoundingRect() );

		x ++;

}

void GraphicsView::mouseMoveEvent ( QMouseEvent * )
{
	if(timeLine->state() != QTimeLine::Running)
	{
		timeLine->setFrameRange(-10,10);
		timeLine->start();
	}
}


void GraphicsView::updateStep ( int step )
{
	//rotate(step/10);
	QRectF r = contentsRect ();
	
//	if ( timeLine->direction() != QTimeLine::Backward )
//	{
		//resetTransform();
		/*
		QTransform t;
		t.translate(r.width() / 2,r.height() / 2);
		t.rotate(angle * step,Qt::YAxis);
		t.translate(-1*r.width() / 2,-1*r.height() / 2);	
		setTransform(t);
		*/

		this->setTransform ( QTransform() 
	                     .translate ( r.width() / 2, 0)
	                     .rotate(step , Qt::ZAxis)
	                   /*  .rotate (angle * step, Qt::YAxis )
	                     /*.rotate(step * 5, Qt::ZAxis)*/
	                     /*.scale ( 1 + 0.5* step, 1+ 0.5* step )*/
	                     .translate ( -r.width() / 2, 0));
						
	//	}
}

void GraphicsView::stateChanged ( QTimeLine::State state )
{

	/*
		if ( state == QTimeLine::Running )
		{
			if ( timeLine->direction() == QTimeLine::Forward )
				setCacheMode ( QGraphicsView::CacheNone );
		}
		else if ( state == QTimeLine::NotRunning )
		{
			if ( timeLine->direction() == QTimeLine::Backward )
			{
				qDebug ( "State : %d",state );
				timeLine->stop();
				setCacheMode ( QGraphicsView::CacheBackground );

			}

		}
	*/
}

void GraphicsView::finished()
{
	//resetTransform();
	qDebug("Animation finshed");
	if ( timeLine->direction() == QTimeLine::Forward )
	{
		timeLine->setFrameRange(-10,10);
		qDebug("Start in backward mode");
		timeLine->setDirection ( QTimeLine::Backward );
		timeLine->start();
	}else
	{
		updateStep(0);
	}


	/*
		if ( iterationOver )
		{
			return;
		}

		if ( timeLine->direction() != QTimeLine::Backward )
		{
			qDebug ( "Direction Set to backword" );
			timeLine->setDirection ( QTimeLine::Backward );
			if ( ( timeLine->state() == QTimeLine::NotRunning ) )
			{
				iterationOver = false;
				timeLine->start();
			}
 
		}
	*/
}
